package com.platform.common.base;

import java.util.Date;
import java.util.Objects;

/**
 * Ajax请求返回的JSON数据
 * 
 * @author wangjun
 *
 * @param <T>
 */
public class BackJsonBean<T> {

	/**
	 * 数据状态 0 失败 1 成功
	 */
	private int status;

	/**
	 * 描述
	 */
	private String desc;

	/**
	 * 路径
	 */
	private String url;

	/**
	 * JSON数据
	 */
	private T data;

	/**
	 * 错误信息摘要(提供给最终用户提示)
	 */
	private String errors;

	/**
	 * 错误信息的详细内容
	 */
	private String errorDetails;

	/**
	 * 影响时间
	 */
	private Long lastTime;

	/**
	 * 请求失败
	 */
	public static final int FAIL = 400;
	/**
	 * 请求成功
	 */
	public static final int SUCCESS = 100;
	/**
	 * 连接数据库出错
	 */
	public static final int ERROR_CONN_ERR = 401;
	/**
	 * 数据库关闭失败
	 */
	public static final int ERROR_CONN_CLOSE_ERR = 402;
	/**
	 * 数据库插入错误
	 */
	public static final int ERROR_INS_ERR = 403;
	/**
	 * 数据库删除错误
	 */
	public static final int ERROR_DEL_ERR = 404;
	/**
	 * 数据库查询错误
	 */
	public static final int ERROR_QUERY_ERR = 405;
	/**
	 * 初始化参数错误
	 */
	public static final int ERROR_DATA_ERR = 406;
	/**
	 * 库存数与预计下限不可同时为空
	 */
	public static final int ERROR_DATA_ERR_Z = 406_1;
	/**
	 * 参数API编码非法
	 */
	public static final int ERROR_API_ERR = 407;
	/**
	 * 参数超长
	 */
	public static final int ERROR_DATA_TOOLONG = 408;
	/**
	 * 没有初始化或初始化失败
	 */
	public static final int ERROR_INIT_ERR = 409;
	/**
	 * API接口处于暂停（失效）状态
	 */
	public static final int ERROR_IFSTATUS_INVALID = 410;
	/**
	 * 数据库更新误
	 */
	public static final int ERROR_UPDATE_ERR = 411;


	public BackJsonBean() {

	}

	public BackJsonBean(int status, String desc) {
		this.status = status ;
		this.desc = Objects.equals(desc, "") ?errorMsg(status):desc;
		this.lastTime=new Date().getTime();
	}
    public BackJsonBean(int status, String desc, String errorDetails) {
        this.status = status ;
        this.errorDetails = errorDetails;
        this.desc = Objects.equals(desc, "") ?errorMsg(status):desc;
			this.lastTime=new Date().getTime();
    }
    public BackJsonBean(int status, String desc, T data) {
        this.status = status ;
        this.desc = Objects.equals(desc, "") ?errorMsg(status):desc;
        this.data=data;
        this.lastTime=new Date().getTime();
    }
	public static int getSUCCESS() {
		return SUCCESS;
	}

	public static int getFAIL() {
		return FAIL;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getErrors() {
		return errors;
	}

	public void setErrors(String errors) {
		this.errors = errors;
	}

	public String getErrorDetails() {
		return errorDetails;
	}

	public void setErrorDetails(String errorDetails) {
		this.errorDetails = errorDetails;
	}

	public Long getLastTime() {
		return lastTime;
	}

	public void setLastTime(Long lastTime) {
		this.lastTime = lastTime;
	}

	public static int getErrorConnErr() {
		return ERROR_CONN_ERR;
	}

	public static int getErrorConnCloseErr() {
		return ERROR_CONN_CLOSE_ERR;
	}

	public static int getErrorInsErr() {
		return ERROR_INS_ERR;
	}

	public static int getErrorDelErr() {
		return ERROR_DEL_ERR;
	}

	public static int getErrorQueryErr() {
		return ERROR_QUERY_ERR;
	}

	public static int getErrorDataErr() {
		return ERROR_DATA_ERR;
	}

	public static int getErrorApiErr() {
		return ERROR_API_ERR;
	}

	public static int getErrorDataToolong() {
		return ERROR_DATA_TOOLONG;
	}

	public static int getErrorInitErr() {
		return ERROR_INIT_ERR;
	}

	public static int getErrorIfstatusInvalid() {
		return ERROR_IFSTATUS_INVALID;
	}

	public static int getErrorDataErrZ() {
		return ERROR_DATA_ERR_Z;
	}

	public static int getErrorUpdateErr() {
		return ERROR_UPDATE_ERR;
	}

	public String errorMsg(int result) {
		switch(result){
			case BackJsonBean.SUCCESS:
				return "请求成功";
				case ERROR_DATA_ERR_Z:
				return "库存数与预计下限不可同时为空";
			case BackJsonBean.FAIL:
				return "请求失败";
			case BackJsonBean.ERROR_API_ERR:
				return "参数API编码非法";
			case BackJsonBean.ERROR_CONN_CLOSE_ERR:
				return "数据库关闭失败";
			case BackJsonBean.ERROR_CONN_ERR:
				return "连接数据库出错";
			case BackJsonBean.ERROR_DATA_ERR:
				return "初始化参数错误";
			case BackJsonBean.ERROR_DEL_ERR:
				return "数据库删除错误";
			case BackJsonBean.ERROR_IFSTATUS_INVALID:
				return "API接口处于暂停（失效）状态";
			case BackJsonBean.ERROR_INIT_ERR:
				return "没有初始化或初始化失败";
			case BackJsonBean.ERROR_INS_ERR:
				return "数据库插入错误";
			case BackJsonBean.ERROR_QUERY_ERR:
				return "数据库查询错误";
			case BackJsonBean.ERROR_DATA_TOOLONG:
				return "参数超长";
			case BackJsonBean.ERROR_UPDATE_ERR:
				return "数据库更新错误";
		}
		return "未知错误";
	}
}
